<template>
  <el-switch
    v-model="data"
    @change="change"
    :disabled="isReadonly"
    :active-value="activeValue ? activeValue : '1'"
    :inactive-value="inactiveValue ? inactiveValue : '0'"
  >
  </el-switch>
</template>
<script>
export default {
  props: {
    value: [String, Number],
    activeValue: [String, Number, Boolean],
    inactiveValue: [String, Number, Boolean],
    isReadonly: {
      type: Boolean,
      default: false,
    },
    formData: Object,
    config: {
      type: Object,
      default: () => Object.create(null),
    },
  },
  created() {},
  data() {
    return {
      data: null,
    };
  },
  computed: {
    curVal() {
      return this.value ? this.value : this.formData[this.config.prop] || "";
    },
  },
  watch: {
    curVal: {
      immediate: true,
      deep: true,
      handler(val) {
        this.data = val;
      },
    },
  },
  methods: {
    change(val) {
      if (this.config.type) {
        this.$set(this.formData, this.config.prop, val);
        this.$emit("update:formData", this.formData);
      } else {
        this.$emit("update:value", this.data);
      }
      this.$emit("change", this.config.prop);
    },
  },
};
</script>
